package com.hoshiicloud.lessee.controller.category;

import com.hoshiicloud.auth.vo.TokenUsersVO;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.token.UserInfoUtil;
import com.hoshiicloud.goods.entity.goods.GoodsSpecificationsValuesType;
import com.hoshiicloud.goods.param.category.GoodsSpecificationsValuesTypeAddParam;
import com.hoshiicloud.goods.param.category.GoodsSpecificationsValuesTypeQueryParam;
import com.hoshiicloud.goods.param.category.GoodsSpecificationsValuesTypeUpdateParam;
import com.hoshiicloud.lessee.feign.LesseeGoodsFeignService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 2019/8/9 16:41 qry
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/category/goods-spec-values-type")
@Api(value = "品类属性-规格值分类", tags = {"品类属性-规格值分类"})
public class LesseeSpecificationsValuesTypeController {
    private final LesseeGoodsFeignService goodsFeignService;
    public LesseeSpecificationsValuesTypeController(LesseeGoodsFeignService goodsFeignService) {
        this.goodsFeignService = goodsFeignService;
    }

    @ApiOperation(value = "分页查询规格值分类")
    @PostMapping("/getPage")
    public Response page(@RequestBody @Valid GoodsSpecificationsValuesTypeQueryParam param, HttpServletRequest rq) {
        param.setCompanyId(UserInfoUtil.getUserInfo(rq).getCompanyId());
        return goodsFeignService.getGoodsSpecificationsValuesTypePage(param);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增规格值类别")
    public Response<Boolean> save(@RequestBody @Valid GoodsSpecificationsValuesTypeAddParam param, HttpServletRequest rq) {
        TokenUsersVO user = UserInfoUtil.getUserInfo(rq);
        param.setCompanyId(user.getCompanyId());
        param.setCreatedBy(user.getRealName());
        return  goodsFeignService.saveGoodsSpecificationsValuesType(param);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改规格值分类")
    public Response<Boolean> update(@RequestBody @Valid GoodsSpecificationsValuesTypeUpdateParam param, HttpServletRequest rq) {
        param.setUpdatedBy( UserInfoUtil.getUserInfo(rq).getRealName());
        return goodsFeignService.updateGoodsSpecificationsValuesType(param);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除规格值分类")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<Boolean> delete(@PathVariable("id") Long id) {
        return goodsFeignService.deleteGoodsSpecificationsValuesType(id);
    }

    @GetMapping("/dropdownBox")
    @ApiOperation(value = "获取下拉框数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "type",value = "规格类型",dataType = "String",example = "尺码")})
    public Response<List<GoodsSpecificationsValuesType>> dropdownBox(@RequestParam("type") @Valid @NotBlank(message = "属性名不能为空") String type,HttpServletRequest rq) {
        Long companyId = UserInfoUtil.getUserInfo(rq).getCompanyId() ;
        return goodsFeignService.goodsSpecificationsValuesTypeDropdownBox(companyId,type);
    }
}
